class Location{
    constructor(x,y){
        this.x=x;
        this.y=y;
    }
    x;
    y;
}

let location_list_area;
let slide;
window.onLoad  = function(){
    let currentReq;                                                             //当前的查询请求
    let currentLocation;                                                        //当前的位置
    let marks=[];                                                               //当前的标注信息
    reqUserLocation();

    /**
     * 向服务器请求用户当前的位置
     */
    function reqUserLocation() {
        let obj={
            openId:1111,
            data:"aaa"
        };
        let bodyStr=JSON.stringify(obj);
        let req=new HttpRequest(Server.reqUserLocation,bodyStr,"POST",15000,function (code, body, json) {
            if (json&&json.position){
                currentLocation=new Location(json.position.x,json.position.y);
            }else {
                currentLocation=new Location(116.394855,39.912046);
            }
            let map=setMap(currentLocation);
            requestNearbyLocation(map,currentLocation);
        });
        req.request();
    }

    /**
     * 搜寻地图某个位置附近的运动场所，在搜寻到的场所的坐标附近画上标志.
     * @param _map  被绘制的地图对象
     * @param _currentLocation  搜寻的中心点位置。
     */
    function requestNearbyLocation(_map,_currentLocation) {
        let obj={
            openId:1111,
            position:{
                north:_currentLocation.left,    //暂未写完
                south:_currentLocation.y,
                west:_currentLocation.y,
                east:_currentLocation.y
            }
        };
        let bodyStr=JSON.stringify(obj);
        let req=new HttpRequest(Server.requestNearbyLocation,bodyStr,"POST",15000,function (code, body,json) {
            currentReq=undefined;
            slide.setValues(json.positions);
            addMark(_map,json.positions);
        });
        currentReq=req;
        req.request();
    }

    /**
     * 查询当前位置旁边的运动场
     * @param _map
     * @param _currentLocation
     */

    /**
     * 使用当前定位初始化地图
     * @param _location
     * @returns {AMap.Map}
     */
    function setMap(_location){
        let map =new AMap.Map('container', {
            resizeEnable: true,
            center: [_location.x, _location.y],
            zoom: 13
        });

        map.on('complete', function(ev) {
            console.info('地图加载结束');

            map.on('zoomend', function(ev) {

                let location=map.getCenter();
                // 触发事件类型
                let type = ev.type;
                console.info('地图缩放结束---事件类型:'+type+'\t事件位置:经度-'+location.getLng()+"\t纬度-"+location.getLat());
                requestNearbyLocation(map,new Location(location.getLng(),location.getLat()));
            });

            map.on('moveend', function(ev) {
                let location=map.getCenter();
                let bounds=map.getBounds();
                let southwest=bounds.southwest;
                let northeast=bounds.northeast;
                // 触发事件类型
                let type = ev.type;
                let boundStr="西南角坐标-经度:"+southwest.getLng()+"\t纬度:"+southwest.getLat()+"\t东北角-经度:"+northeast.getLng()+"\t纬度:"+northeast.getLat();
                console.info('地图移动结束---事件类型:'+type+'\t事件位置:经度-'+location.getLng()+"\t纬度-"+location.getLat());
                console.info('地图两个角'+boundStr);
                requestNearbyLocation(map,new Location(location.getLng(),location.getLat()));
            });
            map.on('movestart', function(ev) {
                if (undefined!==currentReq){
                    currentReq.cancle();
                    console.info("有网络请求，已经取消");
                }else {
                    console.info("当前没有网络请求，不需要取消");
                }
            });

            map.on('resize', function(ev) {
                let location=map.getCenter();

                // 触发事件类型
                let type = ev.type;
                console.info('地图大小发生改变---事件类型:'+type+'\t事件位置:经度-'+location.getLng()+"\t纬度-"+location.getLat());
                requestNearbyLocation(map,new Location(location.getLng(),location.getLat()));
            });
        });
        return map;
    }

    /**
     *  在地图上面添加这几个标记
     * @param locationArray
     * @param map
     */
    function addMark(map,locationArray) {
        for (let i = 0; i <marks.length ; i++) {
            map.remove(marks[i]);
        }
        if (locationArray&&Array.isArray(locationArray)){
            for (let i = 0; i <locationArray.length ; i++) {
                let temp=locationArray[i];
                marks[i] = new AMap.Marker({
                    position: new AMap.LngLat(temp.x,temp.y),   // 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
                    title: temp.name
                });
            }
            map.add(marks);
        }
    }


    function slideChange(index){
        console.info('当前索引为:'+index);
    }
    function layoutLoad(layout,item,index){
        layout.innerHTML=item.name;
    }
    location_list_area=document.getElementById("location_list_area");
    let div=document.createElement("div");
    slide=new Slide(location_list_area,div,['1','2'],slideChange,    layoutLoad);
};
let url = 'https://webapi.amap.com/maps?v=1.4.15&key=4b1de0958162f908ee0728cb50c7c6ad&callback=onLoad';
let jsapi = document.createElement('script');
jsapi.charset = 'utf-8';
jsapi.src = url;
document.head.appendChild(jsapi);